import { ComponentType, useEffect } from "react";
import { useRouter } from "next/navigation";
import useUserStore from "../state/login_state";

const withAuth = (WrappedComponent: ComponentType) => {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const AuthComponent = (props: any) => {
    const token = useUserStore.getState().token;
    const router = useRouter();

    useEffect(() => {
      // 如果token为空，则重定向到登录页
      if (token === "") {
        router.push("/login");
        console.log("token-", token);
      }
    }, [token, router]);

    // 如果用户已登录，渲染传入的组件
    return <WrappedComponent {...props} />;
  };

  return AuthComponent;
};

export default withAuth;
